package com.gopay.solution.service.solu.impl;

import java.io.Serializable;
import java.util.ArrayList;

import org.springframework.stereotype.Service;

import com.gopay.common.domain.solution.SolutionRequest;
import com.gopay.common.domain.solution.SolutionResponse;
import com.gopay.remote.solution.vo.PayChannelDtlInfo;
import com.gopay.common.constants.solution.SolutionTypeConstant;
import com.gopay.solution.domain.po.SolutionRuleDtl;
import com.gopay.solution.domain.po.SsTransPayChanDtl;

/**
 * 
 * @SsTransPayChanServiceImpl.java
 * @author liujy
 * @2013-3-27 下午4:04:35  www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value="ssTransPayChanService")
public class SsTransPayChanServiceImpl extends SolutionServiceImpl {
    
    private ThreadLocal<String> acctIdLocal = new ThreadLocal<String>();

    @Override
    public String getSolutionType() {
        return SolutionTypeConstant.TRANS_PAYCHAN;
    }

    public SolutionRequest beforeProcess(SolutionRequest request) {

        
        
        
        acctIdLocal.set(request.getAcctId());
        
        return request;
    }

    @SuppressWarnings({ "unchecked" })
    public SolutionResponse afterProcess(SolutionResponse response) {

        Serializable s = response.getResponse();
        ArrayList<SolutionRuleDtl> dtls = (ArrayList<SolutionRuleDtl>)s;
        int size = null == dtls ? 0 : dtls.size();
        
        PayChannelDtlInfo[] infos = new PayChannelDtlInfo[size];
        
        String intTxnCd = "";
        
        for(int i = 0; i < size; i++){
            
            SsTransPayChanDtl dtl = (SsTransPayChanDtl)dtls.get(i);
            PayChannelDtlInfo info = new PayChannelDtlInfo();
            info.setOutStlmId(dtl.getOutStlmId());
            info.setPayChannel(dtl.getPayChannel());
            
            //是否个性化标识符 add by fanghw 20151116
            info.setPersonal(dtl.getPersonal());
            
            
            
            infos[i] = info;
            
            if(intTxnCd.equals("")){
                intTxnCd = dtl.getGopayIntTxnCd(); 
            }
        }
        
        response.setResponse(filter(infos, intTxnCd));
        
        
        acctIdLocal.remove();
        
        
        return response;
    }
    
    /**
     * 冗余清算渠道过滤（00500交易，个人用户只显示个人网银，企业只显示企业网银）
     * @param array
     * @param intTxnCd
     * @return
     */
    private PayChannelDtlInfo[] filter(PayChannelDtlInfo[] array, String intTxnCd){
        
        return array;
        
       /* if(null == array || 0 == array.length || !IntTxnCd._00500.value.equals(intTxnCd)){
            return array;
        }
        
        String acctId = acctIdLocal.get();
        String acctType = acctId.substring(8, 10);
        
        List<PayChannelDtlInfo> list_ = new ArrayList<PayChannelDtlInfo>();
        for(PayChannelDtlInfo info : array){
            if("01".equals(acctType)){// 个人的过滤掉企业网银
                if(Integer.valueOf(info.getPayChannel()) <= 50){
                    list_.add(info);
                }
            }else if("02".equals(acctType)){// 企业的过滤掉个人网银
                if(Integer.valueOf(info.getPayChannel()) > 50){
                    list_.add(info);
                }
            }
        }

        int size = null == list_ ? 0 : list_.size();
        PayChannelDtlInfo[] infos = new PayChannelDtlInfo[size];
        for(int i = 0; i < size; i++){
            infos[i] = list_.get(i);
        }
        return infos;*/
    }
}